/*
 * WADS 1.1
 * CoE:2007
 * author: Mr.Nattasan Wang-Aphisit
 */

package wads.app;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class NewUserManagerFrame{
  public static final int USER = 2;
  public static final int PASSWORD = 3;
  public static final int LEVEL = 4;
  public static final int EMAIL = 5;
  
  private static final int UTF_COL = 15;  //User TextField Columns Width
  
  //private static final Dimension vpad7 = new Dimension(1, 7);
  private static final Dimension vpad10 = new Dimension(1, 10);
  private static final Dimension vpad20 = new Dimension(1, 20);
  private static final Dimension hpad7 = new Dimension(7, 1);
  private static final Dimension hpad10 = new Dimension(10, 1);
  private static final Insets insets = new Insets(5, 10, 0, 10);
  
  //GUI Control declaration
  private JFrame jf;
  private JComboBox Username;
  private JPasswordField PWtf;
  private JLabel UsernameLabel, PWLabel, UNameLabel, PosLabel, EmailLabel;
  private JTextField UNametf, Postf, Emailtf;
  private JPanel WindowPanel, UserControlFieldPanel, UserFieldPanel, EastSidePanel, RolePanel, UserControlButtonPanel, WindowControlButtonPanel;
  private JButton AddBtn, DeleteBtn, UpdateBtn, OKBtn, CancelBtn;
  private JCheckBox roleCB01;
  
  public NewUserManagerFrame() {
    initGUI();    
  }
  
  private void initGUI() {
    jf = new JFrame("��á�˹����ҷ�ͧ�ؤ�ҡ�");
    jf.getContentPane().setLayout(new BorderLayout());
    jf.setSize(700, 400);
    WindowPanel = new JPanel();
    WindowPanel.setLayout(new BoxLayout(WindowPanel, BoxLayout.Y_AXIS));
    WindowPanel.setBounds(0, 0, 700, 400);
    
    //UserFieldPanel with all Controls
    UserFieldPanel = new InsetPanel(insets);
    UserFieldPanel.setBorder(BorderFactory.createTitledBorder("��������´�ͧ�����ҹ"));
    UserFieldPanel.setLayout(null);
    UsernameLabel = new JLabel("Username: ");
    UsernameLabel.setBounds(20, 30, 100, 25);
    Username = new JComboBox();
    Username.setBounds(90, 30, 200, 25);
    PWLabel = new JLabel("Password: ");
    PWLabel.setBounds(20, 65, 100, 25);
    PWtf = new JPasswordField(UTF_COL);
    PWtf.setBounds(90, 65, 200, 25);
    UNameLabel = new JLabel("����: ");
    UNameLabel.setBounds(20, 100, 100, 25);
    UNametf = new JTextField(UTF_COL);
    UNametf.setBounds(90, 100, 200, 25);
    PosLabel = new JLabel("���˹�: ");
    PosLabel.setBounds(20, 135, 100, 25);
    Postf = new JTextField(UTF_COL);
    Postf.setBounds(90, 135, 200, 25);
    EmailLabel = new JLabel("E-Mail: ");
    EmailLabel.setBounds(20, 170, 100, 25);
    Emailtf = new JTextField(UTF_COL);
    Emailtf.setBounds(90, 170, 200, 25);
    
    UserFieldPanel.add(UsernameLabel);
    UserFieldPanel.add(Username);
    UserFieldPanel.add(PWLabel);
    UserFieldPanel.add(PWtf);
    UserFieldPanel.add(UNameLabel);
    UserFieldPanel.add(UNametf);
    UserFieldPanel.add(PosLabel);
    UserFieldPanel.add(Postf);
    UserFieldPanel.add(EmailLabel);
    UserFieldPanel.add(Emailtf);
    
    //RolePanel with all Controls
    RolePanel = new InsetPanel(insets);
    RolePanel.setLayout(new BoxLayout(RolePanel, BoxLayout.Y_AXIS));
    roleCB01 = new JCheckBox("���ͺ");
    roleCB01.setToolTipText("Role ���ͺ");
    RolePanel.add(roleCB01);
    
    //ButtonPanel with all Controls
    UserControlButtonPanel = new JPanel();
    UserControlButtonPanel.setLayout(new BoxLayout(UserControlButtonPanel, BoxLayout.X_AXIS));
    
    AddBtn = new JButton("����");
    AddBtn.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        addbtnAP(ae);
      }
    });
    
    DeleteBtn = new JButton("ź");
    UpdateBtn = new JButton("��Ѻ��ا");
    UserControlButtonPanel.add(AddBtn);
    UserControlButtonPanel.add(Box.createRigidArea(hpad7));
    UserControlButtonPanel.add(DeleteBtn);
    UserControlButtonPanel.add(Box.createRigidArea(hpad7));
    UserControlButtonPanel.add(UpdateBtn);
    
    //EastSidePanel
    EastSidePanel = new JPanel();
    EastSidePanel.setBorder(BorderFactory.createTitledBorder("��˹���ҡ�÷ӧҹ"));
    EastSidePanel.setLayout(new BoxLayout(EastSidePanel, BoxLayout.Y_AXIS));
    EastSidePanel.add(RolePanel);
    EastSidePanel.add(UserControlButtonPanel);
    
    //WindowControlPanel with all Controls
    WindowControlButtonPanel = new JPanel();
    WindowControlButtonPanel.setLayout(null);
    OKBtn = new JButton("OK");
    OKBtn.setBounds(370, 10, 80, 30);
    OKBtn.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        jf.setVisible(false);
        jf.dispose();
      }
    });
    
    CancelBtn = new JButton("Cancel");
    CancelBtn.setBounds(460, 10, 80, 30);
    CancelBtn.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        jf.dispose();
      }
    });
    
    WindowControlButtonPanel.add(OKBtn);
    WindowControlButtonPanel.add(Box.createRigidArea(hpad10));
    WindowControlButtonPanel.add(CancelBtn);

    UserControlFieldPanel = new JPanel();
    UserControlFieldPanel.setLayout(new BoxLayout(UserControlFieldPanel, BoxLayout.X_AXIS));
    UserControlFieldPanel.add(Box.createRigidArea(hpad10));
    UserControlFieldPanel.add(UserFieldPanel);
    UserControlFieldPanel.add(Box.createRigidArea(hpad10));
    UserControlFieldPanel.add(EastSidePanel);
    UserControlFieldPanel.add(Box.createRigidArea(hpad10));
    WindowPanel.add(Box.createRigidArea(vpad20));
    WindowPanel.add(UserControlFieldPanel);
    WindowPanel.add(Box.createRigidArea(vpad10));
    WindowPanel.add(WindowControlButtonPanel);
    jf.getContentPane().add(WindowPanel, BorderLayout.CENTER);
    jf.setResizable(false);
    jf.setVisible(true);
    //jf.pack();
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  
  private void addbtnAP(ActionEvent e) {
    String newUser = JOptionPane.showInputDialog(jf, "��س���� Username ����");
    if(checkUsernameValidation(newUser))
      System.out.println("����������");
    else
      System.out.println("�������������");
  }
  
  private boolean checkUsernameValidation(String uname) {
    if(uname != null)
      return true;
    return false;
  }
  
  private class InsetPanel extends JPanel{
//    private static final long serialVersionUID = -242867803915205950L;
    Insets i;
    InsetPanel(Insets i){
      this.i = i;
    }
    @Override
    public Insets getInsets() {
      return this.i;
    }
  }
  
  /*public static void main(String[] args) {
    new NewUserManagerFrame();
  }*/
}
